package com.wyd.controller;

import com.wyd.rabbitmq.domain.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

/**
 * 各种中间件测试
 */
@Slf4j
@RestController
@RequestMapping("/rabbitmq")
public class RabbitmqController {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @RequestMapping(value="testHello",method = RequestMethod.GET)
    public String testHello(@RequestParam(value="queue",defaultValue = "hello") String queue,@RequestParam(value="msg") String msg){
        log.info("收到消息内容：{},{}",queue,msg);
        rabbitTemplate.convertAndSend(queue,new User(1,msg));
        return msg;
    }

    /**
     * 第二模型的消息发送
     */
    @RequestMapping(value="testWork",method = RequestMethod.GET)
    public void testWork(){
        for (int i = 1; i <=10 ; i++) {
            rabbitTemplate.convertAndSend("work","hello work--"+i);
        }
        System.out.println("消息全部发送成功");
    }

    /**
     * 第三种模型的消息发送 fanout
     */
    @RequestMapping(value="testFanout",method = RequestMethod.GET)
    public void testFanout(@RequestParam(value="exchange",defaultValue = "logs") String exchange){
        rabbitTemplate.convertAndSend(exchange,"","这是一个fanout的日志消息");
        System.out.println("消息发送成功");
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    /**
     * 第四种模型(routing-Direct)
     */
    @RequestMapping(value="testRoutingDirect",method = RequestMethod.GET)
    public void testRoutingDirect(){
        rabbitTemplate.convertAndSend("directs","info","info的日志信息");
        rabbitTemplate.convertAndSend("directs","warm","warm的日志信息");
        rabbitTemplate.convertAndSend("directs","debug","debug的日志信息");
        rabbitTemplate.convertAndSend("directs","error","error的日志信息");
        System.out.println("消息发送成功");
    }


    /**
     * 第五种模型(routing-Topic)
     */
    @RequestMapping(value="testTopic",method = RequestMethod.GET)
    public void testTopic(){
        rabbitTemplate.convertAndSend("topics","user.save","user.save的消息");
        rabbitTemplate.convertAndSend("topics","user.save.findAll","user.save.findAll的消息");
        rabbitTemplate.convertAndSend("topics","user","user的消息");
        System.out.println("消息发送成功");
    }

    @RequestMapping(value="testAckMessage",method = RequestMethod.GET)
    public void testAckMessage(@RequestParam(value="msg") String msg) throws Exception {
        rabbitTemplate.convertAndSend("directs", "error", msg,
                new MessagePostProcessor() {
                    @Override
                    public Message postProcessMessage(Message message) throws AmqpException {
                        //自己给消息设置自定义的ID
                        String messageId= UUID.randomUUID().toString().replace("-","");
                        message.getMessageProperties().setMessageId(messageId);
                        return message;
                    }
                });
        System.out.println("消息发送成功");
//        System.in.read();
    }

}
